#include <GL/glut.h>
#include "Cannon.h"
#include "Projectile.h"
#include "stdlib.h"

Cannon::Cannon(float angle, float x, float z)
{
	rotation = angle;
	xTranslation = x;
	zTranslation = z;

	shootTimer = 0;
	cannonTimer = 0;
	shot = false;
	cannonSize = 0.5f;
	cannonAngle = 0.0f;
	myQuad = gluNewQuadric();
}

void Cannon::DrawCannon(GLuint &texture_obj)
{

	//Draws cylinder base
	glPushMatrix();
		glColor3f(0.0f,0.0f,1.0f);
		glTranslatef(xTranslation,0.0,zTranslation);
		glRotatef(90, 1,0,0);
		gluCylinder(myQuad, 0.5f, 0.5f, 1.0f, 20, 20);
	glPopMatrix();

	//Draws rectangle cannon
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D,texture_obj);
	glPushMatrix();
		glColor3f(1.0f,1.0f,1.0f);
		glTranslatef(xTranslation,0.0,zTranslation);
		glRotatef(rotation+cannonAngle,0.0,1.0,0.0);
		glScalef(2.0,1.0,1.0);

		glBegin(GL_QUADS);
	
		//Font
		glNormal3f(0.0f, 0.0f, 1.0f);
		glTexCoord2f (0,1); glVertex3f(-cannonSize, -cannonSize, cannonSize);
		glTexCoord2f (1,1); glVertex3f(cannonSize, -cannonSize, cannonSize);
		glTexCoord2f (1,0); glVertex3f(cannonSize, cannonSize, cannonSize);
		glTexCoord2f (0,0); glVertex3f(-cannonSize, cannonSize, cannonSize);

		//Right
		glNormal3f(1.0f, 0.0f, 0.0f);
		glTexCoord2f (0,1); glVertex3f(cannonSize, -cannonSize, -cannonSize);
		glTexCoord2f (0,0); glVertex3f(cannonSize, cannonSize, -cannonSize);
		glTexCoord2f (1,0); glVertex3f(cannonSize, cannonSize, cannonSize);
		glTexCoord2f (1,1); glVertex3f(cannonSize, -cannonSize, cannonSize);
	
		//Back
		glNormal3f(0.0f, 0.0f, -1.0f);
		glTexCoord2f (0,1); glVertex3f(-cannonSize, -cannonSize, -cannonSize);
		glTexCoord2f (0,0); glVertex3f(-cannonSize, cannonSize, -cannonSize);
		glTexCoord2f (1,0); glVertex3f(cannonSize, cannonSize, -cannonSize);
		glTexCoord2f (1,1); glVertex3f(cannonSize, -cannonSize, -cannonSize);
	
		//Left
		glNormal3f(-1.0f, 0.0f, 0.0f);
		glTexCoord2f (0,1);	glVertex3f(-cannonSize, -cannonSize, -cannonSize);
		glTexCoord2f (1,1); glVertex3f(-cannonSize, -cannonSize, cannonSize);
		glTexCoord2f (1,0); glVertex3f(-cannonSize, cannonSize, cannonSize);
		glTexCoord2f (0,0); glVertex3f(-cannonSize, cannonSize, -cannonSize);
	
		//Top
		glNormal3f(0.0f, 1.0f, 0.0f);
		glTexCoord2f (0,1);	glVertex3f(-cannonSize, cannonSize, -cannonSize);
		glTexCoord2f (1,1); glVertex3f(-cannonSize, cannonSize, cannonSize);
		glTexCoord2f (1,0); glVertex3f(cannonSize, cannonSize, cannonSize);
		glTexCoord2f (0,0); glVertex3f(cannonSize, cannonSize, -cannonSize);

		//Bottom
		glNormal3f(0.0f, -1.0f, 0.0f);
		glTexCoord2f (0,1);	glVertex3f(-cannonSize, -cannonSize, -cannonSize);
		glTexCoord2f (1,1); glVertex3f(-cannonSize, -cannonSize, cannonSize);
		glTexCoord2f (1,0); glVertex3f(cannonSize, -cannonSize, cannonSize);
		glTexCoord2f (0,0); glVertex3f(cannonSize, -cannonSize, -cannonSize);
		glEnd();
	glPopMatrix();
	glDisable (GL_TEXTURE_2D);
}

void Cannon::Shoot()
{
	b.Set(rotation-cannonAngle, xTranslation, zTranslation);
	b.Shoot();
}

float Cannon::GetRotation() {return rotation;}
float Cannon::GetAngle() {return cannonAngle;}
float Cannon::GetX() {return xTranslation;}
float Cannon::GetZ() {return zTranslation;}

int Cannon::Get_shootTimer() {return shootTimer;}
int Cannon::Get_cannonTimer() {return cannonTimer;}
bool Cannon::Get_shot() { return shot; }

void Cannon::Set_cannonAngle(float ang) {cannonAngle = ang;}
void Cannon::SetX(float X) {xTranslation = X;}
void Cannon::SetZ(float Z) {zTranslation = Z;}

void Cannon::Set_shootTimer(int i) {shootTimer = i;}
void Cannon::Set_cannonTimer(int i) {cannonTimer = i;}
void Cannon::Set_shot(bool i) {shot = i;}

float Cannon::GetProjPositionX()
{
	return b.GetXPosition();
}

float Cannon::GetProjPositionZ()
{
	return b.GetZPosition();
}

void Cannon::ResetProjPosition()
{
	b.ResetPosition();
}